/*
 * GameType.java
 *
 * Created on May 7, 2006, 7:39 PM
 */

package poppitplayer.gui;

import simpoppit.gameboard.GameInterface;
import java.io.*;
import javax.swing.*;

/**
 *
 * @author  Andrew
 */
public class GameType extends javax.swing.JFrame {
    
    /** Creates new form GameType */
    public GameType() {
        this.startForm = new Start();
        initComponents();
    }
    
    /** Creates new form GameType */
    public GameType(Start aForm) {
        this.startForm = aForm;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        jButtonNewGame = new javax.swing.JButton();
        jButtonLoadGame = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jRadioButtonDefault = new javax.swing.JRadioButton();
        jRadioButtonCustom = new javax.swing.JRadioButton();
        jTextFieldXVal = new javax.swing.JTextField();
        jTextFieldYVal = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jButtonStartOver = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Choose Game Type");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("gametypeframe");
        jButtonNewGame.setText("Start New Game");
        jButtonNewGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewGameActionPerformed(evt);
            }
        });

        jButtonLoadGame.setText("Load Saved Game");
        jButtonLoadGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoadGameActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButtonDefault);
        jRadioButtonDefault.setSelected(true);
        jRadioButtonDefault.setText("Default Size");
        jRadioButtonDefault.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButtonDefault.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jRadioButtonDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonDefaultActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButtonCustom);
        jRadioButtonCustom.setText("Custom Size");
        jRadioButtonCustom.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButtonCustom.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jRadioButtonCustom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonCustomActionPerformed(evt);
            }
        });

        jTextFieldXVal.setEditable(false);
        jTextFieldXVal.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextFieldXVal.setText("15");
        jTextFieldXVal.setToolTipText("Type numbers only");

        jTextFieldYVal.setEditable(false);
        jTextFieldYVal.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextFieldYVal.setText("10");
        jTextFieldYVal.setToolTipText("Type numbers only");

        jLabel1.setText("Balloons wide (X)");

        jLabel2.setText("Balloons high (Y)");

        jButtonStartOver.setText("Start Over");
        jButtonStartOver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStartOverActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jButtonNewGame)
                                    .add(jRadioButtonDefault)
                                    .add(jRadioButtonCustom))
                                .add(100, 100, 100))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                .add(layout.createSequentialGroup()
                                    .add(jTextFieldXVal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(jLabel1)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .add(jTextFieldYVal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(jLabel2))
                                .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE))))
                    .add(layout.createSequentialGroup()
                        .add(111, 111, 111)
                        .add(jButtonStartOver))
                    .add(layout.createSequentialGroup()
                        .add(87, 87, 87)
                        .add(jButtonLoadGame))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(24, 24, 24)
                .add(jButtonNewGame)
                .add(20, 20, 20)
                .add(jRadioButtonDefault)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jRadioButtonCustom)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel1)
                        .add(jTextFieldXVal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jTextFieldYVal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jLabel2)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButtonLoadGame)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 17, Short.MAX_VALUE)
                .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(14, 14, 14)
                .add(jButtonStartOver)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonLoadGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoadGameActionPerformed
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(this);
        
        if (returnVal == JFileChooser.APPROVE_OPTION){
            File file = fc.getSelectedFile();
            //System.out.println("Loading file " + file.getAbsolutePath());
            startForm.aGame.loadGame(file.getAbsolutePath());
            startForm.gameForm.loadGameIntoGUI();
            this.setVisible(false);
            startForm.gameForm.setVisible(true);
        }else{
            System.out.println("User cancelled load.");
        }
    }//GEN-LAST:event_jButtonLoadGameActionPerformed

    private void jButtonStartOverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStartOverActionPerformed
        this.setVisible(false);
        startForm.setVisible(true);
    }//GEN-LAST:event_jButtonStartOverActionPerformed

    private void jButtonNewGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewGameActionPerformed
        startForm.aGame = new GameInterface(Integer.decode(this.jTextFieldXVal.getText()), Integer.decode(this.jTextFieldYVal.getText())) ;
        this.setVisible(false);
        startForm.gameForm.loadGameIntoGUI();
        startForm.gameForm.setVisible(true);
    }//GEN-LAST:event_jButtonNewGameActionPerformed

    private void jRadioButtonDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonDefaultActionPerformed
        this.jTextFieldXVal.setText("15");
        this.jTextFieldXVal.setEditable(false);
        this.jTextFieldYVal.setText("10");
        this.jTextFieldYVal.setEditable(false);
    }//GEN-LAST:event_jRadioButtonDefaultActionPerformed
   
    private void jRadioButtonCustomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonCustomActionPerformed
        this.jTextFieldXVal.setEditable(true);
        this.jTextFieldYVal.setEditable(true);
    }//GEN-LAST:event_jRadioButtonCustomActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GameType().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonLoadGame;
    private javax.swing.JButton jButtonNewGame;
    private javax.swing.JButton jButtonStartOver;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JRadioButton jRadioButtonCustom;
    private javax.swing.JRadioButton jRadioButtonDefault;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField jTextFieldXVal;
    private javax.swing.JTextField jTextFieldYVal;
    // End of variables declaration//GEN-END:variables
    private Start startForm;
    public String gameType = "human";
}
